package main
import (
    "encoding/json"
    "fmt"
    "os"
    
)
type BinStruct struct{
	FileName string
    Addr uint32
}

type Cfg struct {
   	Manufacturers uint32
    Product uint32
    Bin []BinStruct
}

var UserCfg Cfg

var cfile = "config.json"
func printCfg() {
	fmt.Printf("读取配置文件\"%s\"成功\r\n", cfile)
	fmt.Printf("Manufacturers\t: 0x%08X\r\n", UserCfg.Manufacturers)
	fmt.Printf("Product\t\t: 0x%08X\r\n", UserCfg.Product)
//	fmt.Printf("Boot\t\t: %s\t0x%08X\r\n", UserCfg.Bin[0].FileName, UserCfg.Bin[0].Addr)
//	fmt.Printf("App0\t\t: %s\t0x%08X\r\n", UserCfg.Bin[1].FileName, UserCfg.Bin[1].Addr)

	for n, d := range UserCfg.Bin {
		fmt.Printf("#%d \t\t: %s\t0x%08X\r\n",n ,d.FileName, d.Addr)
    }
}

func init() {
    fd, err := os.Open(cfile)
	if err != nil {
		fmt.Println("open %s faild", cfile)
		os.Exit(1)
	}
    defer fd.Close()
    decoder := json.NewDecoder(fd)
    err = decoder.Decode(&UserCfg)
    if err != nil {
        fmt.Println("parse %s faild", cfile)
		os.Exit(1)
    }
    for n,i := range UserCfg.Bin {
		fmt.Println(n, i)

    }
    fmt.Println(UserCfg)
    printCfg()
}
func main() {
}
